'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

exports.default = count;

var _helpers = require('./helpers');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function count(model, tc, opts) {
  if (!model || !model.modelName || !model.schema) {
    throw new Error('First arg for Resolver count() should be instance of Mongoose Model.');
  }

  if (!tc || tc.constructor.name !== 'TypeComposer') {
    throw new Error('Second arg for Resolver count() should be instance of TypeComposer.');
  }

  return new tc.constructor.schemaComposer.Resolver({
    type: 'Int',
    name: 'count',
    kind: 'query',
    args: (0, _extends3.default)({}, (0, _helpers.filterHelperArgs)(tc, model, (0, _extends3.default)({
      filterTypeName: `Filter${tc.getTypeName()}Input`,
      model
    }, opts && opts.filter))),
    resolve: resolveParams => {
      resolveParams.query = model.find();
      (0, _helpers.filterHelper)(resolveParams);
      return resolveParams.query.count().exec();
    }
  });
}
/* eslint-disable no-param-reassign */